package secondriver.springsubway.example.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class HelloWorldMultiActionController extends MultiActionController {

	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) {
		return makeModelAndView(request, response, " From index");
	}

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) {
		return makeModelAndView(request, response, " From list");
	}

	private ModelAndView makeModelAndView(HttpServletRequest request,
			HttpServletResponse response, Object object) {
		Map<String, String> model = new HashMap<String, String>(5);
		String name = request.getParameter("name");
		if (StringUtils.isEmpty(name)) {
			model.put("name", "Spring Framework MVC");
		} else {
			model.put("name", name + object);
		}
		model.put("time", new Date().toString());
		ModelAndView modelAndView = new ModelAndView("index", model);
		return modelAndView;
	}
}
